package com.hnevn.newslist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hnevn.newslist.Apdater.NewsAdapter;
import com.hnevn.newslist.Bean.NewsBean1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    ListView mLvItem;
    private Handler handler;
    NewsBean1 newsBean1;
    List<NewsBean1.ResultDTO.DataDTO> list = new ArrayList<>();
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        initView();
        initData();
    }

    private void initData() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            OkHttpClient okHttpClient = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("key","368cf096b8c3c4b53dbc664c686b3a6d")
                    .build();
            Request request = new Request.Builder()
                    .url(" http://v.juhe.cn/toutiao/index?type=top&key=368cf096b8c3c4b53dbc664c686b3a6d")
                    .post(requestBody)
                    .build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(!response.isSuccessful()){
                        Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                    Gson gson = new Gson();

                    try {
                        newsBean1 = gson.fromJson(response.body().string(),NewsBean1.class);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            list.addAll(newsBean1.getResult().getData());
                            NewsAdapter newsAdapter = new NewsAdapter(MainActivity.this,list);
                            mLvItem.setAdapter(newsAdapter);
                        }
                    });
                }
            });
        }
    }).start();
    }

    private void initView() {
        mLvItem = findViewById(R.id.lv_item);
    }
}